#!/bin/sh

set -e

if [[ -z "$DRACUT_TMPDIR" ]]; then
	echo "DRACUT_TMPDIR is unset, exiting"
	exit 0
fi

# We have to unset "prefix" as dracut uses it to move files around.
# Yocto has it set as part of the build environment.
export prefix=

# DRACUT settings
export UDEVVERSION=241
export SYSTEMD_VERSION=241
export DRACUT_TESTBIN=/bin/bash.bash
export DRACUT_INSTALL_PATH="/usr/sbin:/usr/bin:/sbin:/bin"

# PLYMOUTH settings
export PLYMOUTH_SYSROOT=$D

export \
	systemdutildir systemdsystemunitdir systemdsystemconfdir udevdir \
	DRACUT_ARCH DRACUT_TESTBIN DRACUT_LDD DRACUT_LDCONFIG DRACUT_INSTALL \
	DRACUT_COMPRESS_GZIP DRACUT_COMPRESS_BZIP2 DRACUT_COMPRESS_LZMA \
	PLYMOUTH_LDD PLYMOUTH_LDD_PATH PLYMOUTH_PLUGIN_PATH \
	PLYMOUTH_THEME_NAME PLYMOUTH_THEME

# List environment for debugging
echo ROOTFS-POSTINST-ENVIRONMENT
echo -----------
env
echo -----------

[[ -d $DRACUT_TMPDIR ]] || mkdir -p $DRACUT_TMPDIR

# Execute depmod and dracut
echo "$(date) - RUNNING: depmod -b $D -a $MY_KERNEL_VERSION"
depmod -b $D -a $MY_KERNEL_VERSION
echo "dracut: $(dracut --sysroot $D --help | grep 'Version:')"
echo "$(date) - RUNNING: dracut -f --sysroot $D --tmpdir $DRACUT_TMPDIR $DRACUT_OPTS $DEBUGOPTS -k $D/lib/modules/$MY_KERNEL_VERSION $D/boot/initramfs.img $MY_KERNEL_VERSION"
dracut -f --sysroot $D --tmpdir $DRACUT_TMPDIR $DRACUT_OPTS $DEBUGOPTS -k $D/lib/modules/$MY_KERNEL_VERSION $D/boot/initramfs.img $MY_KERNEL_VERSION
echo "$(date) - DONE: dracut"
